/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pub.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pub.entity.PubAffect;
import com.thinkgem.jeesite.modules.pub.entity.PubWelfare;
import com.thinkgem.jeesite.modules.pub.service.PubWelfareService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/pub/pubWelfare")
public class PubWelfareController extends BaseController {

	@Autowired
	private PubWelfareService pubWelfareService;
	
	@ModelAttribute
	public PubWelfare get(@RequestParam(required=false) String id) {
		PubWelfare entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pubWelfareService.get(id);
		}
		if (entity == null){
			entity = new PubWelfare();
		}
		return entity;
	}
	
	@RequiresPermissions("pub:pubWelfare:view")
	@RequestMapping(value = {"list", ""})
	public String list(PubWelfare pubWelfare, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PubWelfare> page = pubWelfareService.findPage(new Page<PubWelfare>(request, response), pubWelfare); 
		model.addAttribute("page", page);
		return "modules/pub/pubWelfareList";
	}

	@RequiresPermissions("pub:pubWelfare:view")
	@RequestMapping(value = "form")
	public String form(PubWelfare pubWelfare, Model model) {
		model.addAttribute("pubWelfare", pubWelfare);
		return "modules/pub/pubWelfareForm";
	}

	@RequiresPermissions("pub:pubWelfare:edit")
	@RequestMapping(value = "save")
	public String save(PubWelfare pubWelfare, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pubWelfare)){
			return form(pubWelfare, model);
		}
		pubWelfareService.save(pubWelfare);
		addMessage(redirectAttributes, "保存公益活动成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubWelfare/?repage";
	}
	
	@RequiresPermissions("pub:pubWelfare:edit")
	@RequestMapping(value = "delete")
	public String delete(PubWelfare pubWelfare, RedirectAttributes redirectAttributes) {
		pubWelfareService.delete(pubWelfare);
		addMessage(redirectAttributes, "删除公益活动成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubWelfare/?repage";
	}
	
	@RequestMapping(value="wxlist")
	public String wxlist(PubWelfare pubWelfare, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Object[]> page = pubWelfareService.welfareList(); 
//		model.addAttribute("page", page);
		return renderString(response, page);
	}
	@RequestMapping(value="wxget")
	public String wxget(HttpServletRequest request, HttpServletResponse response,String id) {
		PubWelfare entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pubWelfareService.get(id);
		}
		if (entity == null){
			entity = new PubWelfare();
		}
		return renderString(response, entity);
	}
	
	@RequestMapping(value="wxsave")
	public String wxsave(HttpServletRequest request, HttpServletResponse response,String title,String authorname,String article,String imgurl,String authorid,String videourl,String category) {
		String id = UUID.randomUUID().toString().replace("-", "");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String publishtime = df.format(new Date());
		int result = pubWelfareService.wxsave(id,title,authorname,article,imgurl,authorid,publishtime,videourl,category);
		
		return renderString(response, result);
	}
	@RequestMapping(value="wxlistbytype")
	public String wxlistbytype(PubWelfare pubWelfare, HttpServletRequest request, HttpServletResponse response, Model model,String category) {
		List<Object[]> page = pubWelfareService.welfareListBytype(category); 
//		model.addAttribute("page", page);
		return renderString(response, page);
	}
	

}